﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using VIBlend.WPF.Controls;

namespace AnonsaiWPF.CSharp.DataGridViewDemos
{
    [Category("VIBlend.WPF.Examples")]
    public partial class CellsStyling : Window, IWindow
    {
        DataTemplate cellTemplateItalicText;
        DataTemplate currencyCellDataTemplate;
        DataTemplate cellTemplateTitleText;
        DataTemplate cellTemplateBoldText;
        Style cellStyleOrangeBackground;
        Style cellStyleGreenBackground;
      
        public void SetTheme()
        {
            dataGrid.FontFamily = new FontFamily("Tahoma");
        }
  
        #region IWindow Members

        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

        public Grid Root
        {
            get
            {
                return this.LayoutRoot;
            }
        }
        #endregion

        public CellsStyling()
        {
            InitializeComponent();

            this.cellTemplateItalicText = this.Resources["cellTemplateItalicText"] as DataTemplate;
            this.currencyCellDataTemplate = this.Resources["currencyCellDataTemplate"] as DataTemplate;
            this.cellTemplateTitleText = this.Resources["cellTemplateTitleText"] as DataTemplate;
            this.cellTemplateBoldText = this.Resources["cellTemplateBoldText"] as DataTemplate;
            this.cellStyleOrangeBackground = this.Resources["cellStyleOrangeBackground"] as Style;
            this.cellStyleGreenBackground = this.Resources["cellStyleGreenBackground"] as Style;

            dataGrid.ColumnsHierarchy.AllowResize = false;
            dataGrid.RowsHierarchy.AllowResize = false;

            LoadGridData();
        }

        private void LoadGridData()
        {
            // Set the data grid in update mode
            dataGrid.BeginUpdate();

            // Create 1000 Rows
            for (int i = 1; i <= 100; i++)
            {
                HierarchyItem item = dataGrid.RowsHierarchy.Items.Add(i.ToString());
                item.Width = 40;
            }

            // Add Columns from 'A' to 'Z'
            for (char c = 'A'; c <= 'Z'; c++)
            {
                HierarchyItem item = dataGrid.ColumnsHierarchy.Items.Add(c.ToString());
                if (c == 'A' || c == 'D')
                    item.Width = 130;
                else if (c == 'B' || c == 'E')
                    item.Width = 90;
                else
                    item.Width = 70;
            }

            // Make the Rows and Columns resizable
            dataGrid.RowsHierarchy.AllowResize = true;
            dataGrid.ColumnsHierarchy.AllowResize = true;
            dataGrid.AllowCellMerge = true;

            // Merge the first 5 columns in in the first row
            dataGrid.CellsArea.SetCellSpan(dataGrid.RowsHierarchy.Items[0], dataGrid.ColumnsHierarchy.Items[0], 1, 5);

            // Set the style of cell(2,A) to a Cell style with orange background (defined in the XAML file)
            dataGrid.CellsArea.SetCellStyle(dataGrid.RowsHierarchy.Items[0], dataGrid.ColumnsHierarchy.Items[0], cellStyleOrangeBackground);

            // Set the data template of the first cell to a TitleText template (defined in the XAML file)
            dataGrid.CellsArea.SetCellDataTemplate(dataGrid.RowsHierarchy.Items[0], dataGrid.ColumnsHierarchy.Items[0], cellTemplateTitleText);

            // Set the value of the first grid cell
            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[0], dataGrid.ColumnsHierarchy.Items[0], "Monthly Housing and Transportation Expenses");


            // Merge the first 2 columns in the second row
            dataGrid.CellsArea.SetCellSpan(dataGrid.RowsHierarchy.Items[1], dataGrid.ColumnsHierarchy.Items[0], 1, 2);

            // Set the style of cell(2,A) to a Cell style with green background (defined in the XAML file)
            dataGrid.CellsArea.SetCellStyle(dataGrid.RowsHierarchy.Items[1], dataGrid.ColumnsHierarchy.Items[0], cellStyleGreenBackground);

            // Set the data template of cell(2,A) to Bold text
            dataGrid.CellsArea.SetCellDataTemplate(dataGrid.RowsHierarchy.Items[1], dataGrid.ColumnsHierarchy.Items[0], cellTemplateBoldText);

            // Set the value of cell(2,A)
            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[1], dataGrid.ColumnsHierarchy.Items[0], "Primary residence");

            // Merge columns C and D in the second row
            dataGrid.CellsArea.SetCellSpan(dataGrid.RowsHierarchy.Items[1], dataGrid.ColumnsHierarchy.Items[3], 1, 2);

         //   dataGrid.CellsArea.SetCellSpan(dataGrid.RowsHierarchy.Items[1], dataGrid.ColumnsHierarchy.Items[3], 1, 1);
           
            // Set the style of cell(2,C) to a Cell style with green background (defined in the XAML file)
            dataGrid.CellsArea.SetCellStyle(dataGrid.RowsHierarchy.Items[1], dataGrid.ColumnsHierarchy.Items[3], cellStyleGreenBackground);

            // Set the data template of cell(2,C) to Bold text
            dataGrid.CellsArea.SetCellDataTemplate(dataGrid.RowsHierarchy.Items[1], dataGrid.ColumnsHierarchy.Items[3], cellTemplateBoldText);

            // Set the value of cell(2,C)
            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[1], dataGrid.ColumnsHierarchy.Items[3], "Transportation Expenses");

            // Set the template for all cells under column 'A' and 'D'
            dataGrid.ColumnsHierarchy.Items[0].CellDataTemplate = cellTemplateItalicText;
            dataGrid.ColumnsHierarchy.Items[3].CellDataTemplate = cellTemplateItalicText;

            // Set the template for the cells in column 'B' and 'E' to currency 
            dataGrid.ColumnsHierarchy.Items[1].CellDataTemplate = currencyCellDataTemplate;
            dataGrid.ColumnsHierarchy.Items[4].CellDataTemplate = currencyCellDataTemplate;

            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[2], dataGrid.ColumnsHierarchy.Items[0], "Mortgage Payment");
            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[2], dataGrid.ColumnsHierarchy.Items[1], 1459.76);

            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[3], dataGrid.ColumnsHierarchy.Items[0], "Property Tax");
            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[3], dataGrid.ColumnsHierarchy.Items[1], 212.54);

            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[4], dataGrid.ColumnsHierarchy.Items[0], "Insurance");
            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[4], dataGrid.ColumnsHierarchy.Items[1], 49.21);

            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[5], dataGrid.ColumnsHierarchy.Items[0], "Electricity");
            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[5], dataGrid.ColumnsHierarchy.Items[1], 73.44);

            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[6], dataGrid.ColumnsHierarchy.Items[0], "Water");
            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[6], dataGrid.ColumnsHierarchy.Items[1], 41.48);

            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[7], dataGrid.ColumnsHierarchy.Items[0], "Cabel TV Service");
            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[7], dataGrid.ColumnsHierarchy.Items[1], 22.14);

            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[8], dataGrid.ColumnsHierarchy.Items[0], "High speed Internet");
            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[8], dataGrid.ColumnsHierarchy.Items[1], 24.99);


            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[2], dataGrid.ColumnsHierarchy.Items[3], "Vehicle 1 payment");
            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[2], dataGrid.ColumnsHierarchy.Items[4], 351.34);

            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[3], dataGrid.ColumnsHierarchy.Items[3], "Vehicle 1 Insurance");
            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[3], dataGrid.ColumnsHierarchy.Items[4], 55.12);

            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[4], dataGrid.ColumnsHierarchy.Items[3], "Vehicle 1 Gas");
            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[4], dataGrid.ColumnsHierarchy.Items[4], 129.21);

            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[5], dataGrid.ColumnsHierarchy.Items[3], "Vehicle 1 Maintenance");
            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[5], dataGrid.ColumnsHierarchy.Items[4], 51.17);

            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[6], dataGrid.ColumnsHierarchy.Items[3], "Vehicle 2 Lease");
            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[6], dataGrid.ColumnsHierarchy.Items[4], 311.12);

            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[7], dataGrid.ColumnsHierarchy.Items[3], "Vehicle 2 Insurance");
            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[7], dataGrid.ColumnsHierarchy.Items[4], 109.35);

            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[8], dataGrid.ColumnsHierarchy.Items[3], "Vehicle 2 Gas");
            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[8], dataGrid.ColumnsHierarchy.Items[4], 114.99);

            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[9], dataGrid.ColumnsHierarchy.Items[3], "Vehicle 2 Maintenance");
            dataGrid.CellsArea.SetCellValue(dataGrid.RowsHierarchy.Items[9], dataGrid.ColumnsHierarchy.Items[4], 35.19);

            // End the data grid update
            dataGrid.EndUpdate();

            // Refresh the data grid
            dataGrid.Refresh();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            this.dataGrid.Dispose();
            this.dataGrid = null;
        }
    }
}
